unbind C-b
unbind q

# Set prefix
set -g prefix M-space

# Set base window index
set -g base-index 1

# Set base pane index
setw -g pane-base-index 1

# Increase scroll-back history
set -g history-limit 10000

# Main pane size
set -g main-pane-height 27
set -g main-pane-width 105

# Setup vi mode
setw -g mode-keys vi

# Renumber window index closing a window
set -g renumber-windows on

# Enable mouse mode
set -g mouse on

# don't rename windows automatically
set-option -g allow-rename off

# Color config
set -g default-terminal "screen-256color"

# Delay for keybindings
set -sg escape-time 0

# Theming
source ~/.config/tmux/themes/rubber.conf

# Command alias
set -s command-alias[0] goto='switch-client -t'
set -s command-alias[1] music='new-session -s music cmus'
set -s command-alias[2] next-pomd='run-shell "dunstctl close && tmux send-keys -t main:gone y"'

# Mappings

# Go to 'command mode'
bind -n F12 command-prompt

# Toggle 'secondary pane'
bind -n F4 last-pane \; if -F '#{==:#{pane_index},1}' 'resize-pane -Z'

# Move to last active pane
bind -n 'M-;' last-pane -Z

# Go to last active window
bind -n M-o last-window

# Switch to last active session
bind -n F2 switch-client -l

# Enter copy mode
bind -n M-q copy-mode

# Use v to trigger selection
bind -T copy-mode-vi v send-keys -X begin-selection

# Use y to yank current selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xsel -i --clipboard'

# Open new window with currrent path
bind c new-window -c '#{pane_current_path}'

# Open new session
bind n command-prompt -p "new-session: " "new -s '%%' -c '#{pane_current_path}'"

# Manage windows (and panes)
bind -T root     M-w    switch-client -T windows
bind -T windows  q      kill-pane
bind -T windows  i      new-window -c '#{pane_current_path}'
bind -T windows  v      split-window -h -c '#{pane_current_path}'
bind -T windows  s      split-window -v -c '#{pane_current_path}'
bind -T windows  t      select-layout main-vertical
bind -T windows  u      select-layout main-horizontal
bind -T windows  M-v    split-window -h -c '#{pane_current_path}' \; select-layout main-vertical
bind -T windows  M-s    split-window -v -c '#{pane_current_path}' \; select-layout main-horizontal
bind -T windows  ';'    last-window
bind -T windows  w      display-panes -d 0
bind -T windows  k      select-pane -U
bind -T windows  j      select-pane -D
bind -T windows  h      select-pane -L
bind -T windows  l      select-pane -R
bind -T windows  g      set status

# Navigate windows
bind -r -T windows  b  previous-window
bind -r -T windows  f  next-window
bind -T windows  1  select-window -t :=1
bind -T windows  2  select-window -t :=2
bind -T windows  3  select-window -t :=3
bind -T windows  4  select-window -t :=4
bind -T windows  5  select-window -t :=5
bind -T windows  6  select-window -t :=6
bind -T windows  7  select-window -t :=7
bind -T windows  8  select-window -t :=8
bind -T windows  9  select-window -t :=9

# Resize panes
bind -T windows   e   switch-client -T r-windows
bind -T r-windows k  resize-pane -U 2 \; switch-client -T r-windows
bind -T r-windows j  resize-pane -D 2 \; switch-client -T r-windows
bind -T r-windows h  resize-pane -L 2 \; switch-client -T r-windows
bind -T r-windows l  resize-pane -R 2 \; switch-client -T r-windows

# Swap panes interactively
bind -T windows  o  choose-tree -Z {
  set -sF @currentpane "#{pane_id}";
  run-shell 'tmux set -s @nextpane "$(tmux display-message -t "%1" -p "##{pane_id}")"';
  swap-pane -t '%1'
}

# Swap with last selected pane
bind -T windows  M-o {
  run-shell '\
    export CURRENT="$(tmux show -sv @nextpane)"; \
    export NEXT="$(tmux show -sv @currentpane)"; \
    tmux set -s @nextpane "$NEXT"; \
    tmux set -s @currentpane "$CURRENT"; \
    tmux swap-pane -d -s "$CURRENT" -t "$NEXT";'
}

set-hook -g session-created {
  if -F '#{==:#{session_name},main}' {set status}
  if -F '#{==:#{session_name},music}' {set status}
  if -F '#{==:#{session_name},vi}' {set status-position top}
}

